package com.ruijie.acl;

import com.ruijie.acl.utils.AclUtil;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.acl.type.PortTypeEnum;
/**
 * <p>
 * Title: ACE对象
 * </p>
 * <p>
 * Description: 用于数据库中的ACE表管理
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class Ace extends EntityObject {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2662277741776897554L;
	private Template template;
	private TimeRange timeRange;
	private AccessGroup accessGroup;
	private Integer aceNum = 0;
	private String actionType;
	private String remark;
	private String protocolType;
	private String vlanNum;
	private String sourceMatchType;
	private String sourceAddr;
	private String sourceWildcard;
	private String sourceMacMatchType;
	private String sourceMacAddr;
	private String sourcePort;
	private String desMatchType;
	private String desAddr;
	private String desWildcard;
	private String desMacMatchType;
	private String desMacAddr;
	private String desPort;
	private String ethernetType;
	private Integer orderNum;
	private String timeRangeName;

	public String getTimeRangeName() {
		return timeRangeName;
	}

	public void setTimeRangeName(String timeRangeName) {
		this.timeRangeName = timeRangeName;
	}

	public Ace() {
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public TimeRange getTimeRange() {
		return timeRange;
	}

	public void setTimeRange(TimeRange timeRange) {
		this.timeRange = timeRange;
	}

	public Integer getAceNum() {
		return this.aceNum;
	}

	public void setAceNum(Integer aceNum) {
		this.aceNum = aceNum;
	}

	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getProtocolType() {
		return this.protocolType;
	}

	public void setProtocolType(String protocolType) {
		this.protocolType = protocolType;
	}

	public String getVlanNum() {
		return this.vlanNum;
	}

	public void setVlanNum(String vlanNum) {
		this.vlanNum = vlanNum;
	}

	public String getSourceMatchType() {
		return this.sourceMatchType;
	}

	public void setSourceMatchType(String sourceMatchType) {
		this.sourceMatchType = sourceMatchType;
	}

	public String getSourceAddr() {
		return this.sourceAddr;
	}

	public void setSourceAddr(String sourceAddr) {
		this.sourceAddr = sourceAddr;
	}

	public String getSourceMacMatchType() {
		return this.sourceMacMatchType;
	}

	public void setSourceMacMatchType(String sourceMacMatchType) {
		this.sourceMacMatchType = sourceMacMatchType;
	}

	public String getSourceMacAddr() {
		return this.sourceMacAddr;
	}

	public void setSourceMacAddr(String sourceMacAddr) {
		this.sourceMacAddr=sourceMacAddr;
	}

	public String getSourcePort() {
		return this.sourcePort;
	}

	public void setSourcePort(String sourcePort) {
		this.sourcePort = sourcePort;
	}

	public String getDesMatchType() {
		return this.desMatchType;
	}

	public void setDesMatchType(String desMatchType) {
		this.desMatchType = desMatchType;
	}

	public String getDesAddr() {
		return this.desAddr;
	}

	public void setDesAddr(String desAddr) {
		this.desAddr = desAddr;
	}

	public String getDesMacMatchType() {
		return this.desMacMatchType;
	}

	public void setDesMacMatchType(String desMacMatchType) {
		this.desMacMatchType = desMacMatchType;
	}

	public String getDesMacAddr() {
		return this.desMacAddr;
	}
	
	public void setDesMacAddr(String desMacAddr) {
		this.desMacAddr=desMacAddr;
	}

	public String getDesPort() {
		return this.desPort;
	}

	public void setDesPort(String desPort) {
		this.desPort = desPort;
	}

	public Integer getOrderNum() {
		return this.orderNum;
	}

	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}

	public AccessGroup getAccessGroup() {
		return accessGroup;
	}

	public void setAccessGroup(AccessGroup accessGroup) {
		this.accessGroup = accessGroup;
	}

	public String getEthernetType() {
		return ethernetType;
	}

	public void setEthernetType(String ethernetType) {
		this.ethernetType = ethernetType;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public String getSourceWildcard() {
		return sourceWildcard;
	}

	public void setSourceWildcard(String sourceWildcard) {
		this.sourceWildcard = sourceWildcard;
	}

	public String getDesWildcard() {
		return desWildcard;
	}

	public void setDesWildcard(String desWildcard) {
		this.desWildcard = desWildcard;
	}
	
	private String convert2String(Object obj){
		return obj == null ? "" : obj.toString();
	}

	public static void main(String[] args){
		System.out.println("".hashCode()+","+new String("dd").hashCode());
		Ace a1=new Ace();
		a1.setDesAddr(new String("dd"));
		Ace a2=new Ace();
		a2.setDesAddr("dd");
		System.out.println(a1.equals(a2));
		java.util.List<Ace> aces=new java.util.ArrayList<Ace>();
		aces.add(a1);
		System.out.println(aces.indexOf(a2));
	}
	
	@Override
	public int hashCode() {
		int hashCode=
			AclUtil.getStringHashCode(this.actionType)+
			AclUtil.getStringHashCode(this.desAddr)+
			AclUtil.getStringHashCode(this.desMatchType)+
			AclUtil.getStringHashCode(this.desMacAddr)+
			AclUtil.getStringHashCode(this.desMacMatchType)+
			AclUtil.getStringHashCode(this.desPort)+
			AclUtil.getStringHashCode(this.desWildcard)+
			AclUtil.getStringHashCode(this.ethernetType)+
			AclUtil.getStringHashCode(this.protocolType)+
			AclUtil.getStringHashCode(this.sourceAddr)+
			AclUtil.getStringHashCode(this.sourceMacAddr)+
			AclUtil.getStringHashCode(this.sourceMacMatchType)+
			AclUtil.getStringHashCode(this.sourceMatchType)+
			AclUtil.getStringHashCode(this.sourcePort)+
			AclUtil.getStringHashCode(this.sourceWildcard)+
			AclUtil.getStringHashCode(this.vlanNum)+
			AclUtil.getStringHashCode(this.timeRangeName);
		return hashCode;
	}

	/**
	 * 严格对比两条规则，要求每个栏位的值要一模一样(包含描述)
	 * @param obj
	 * @return
	 */
	public boolean equalsStrict(Object obj){
		if(obj==this){
			return true;
		}
		if(obj==null||!(obj instanceof Ace)){
			return false;
		}
		Ace ace=(Ace)obj;
		boolean same=
			(aceNum==ace.aceNum||aceNum!=null&&ace.aceNum!=null&&aceNum.intValue()==ace.aceNum.intValue())&&
			AclUtil.equalString(this.actionType,ace.actionType)&&
			AclUtil.equalString(this.desAddr,ace.desAddr)&&
			AclUtil.equalString(this.desMatchType,ace.desMatchType)&&
			AclUtil.equalString(this.desMacAddr,ace.desMacAddr)&&
			AclUtil.equalString(this.desMacMatchType,ace.desMacMatchType)&&
			AclUtil.equalString(this.desPort,ace.desPort)&&
			AclUtil.equalString(this.desWildcard,ace.desWildcard)&&
			AclUtil.equalString(this.ethernetType,ace.ethernetType)&&
			AclUtil.equalString(this.protocolType,ace.protocolType)&&
			AclUtil.equalString(this.sourceAddr,ace.sourceAddr)&&
			AclUtil.equalString(this.sourceMacAddr,ace.sourceMacAddr)&&
			AclUtil.equalString(this.sourceMacMatchType,ace.sourceMacMatchType)&&
			AclUtil.equalString(this.sourceMatchType,ace.sourceMatchType)&&
			AclUtil.equalString(this.sourcePort,ace.sourcePort)&&
			AclUtil.equalString(this.sourceWildcard,ace.sourceWildcard)&&
			AclUtil.equalString(this.vlanNum,ace.vlanNum)&&
			AclUtil.equalString(this.timeRangeName,ace.timeRangeName)&&
			AclUtil.equalString(this.remark, ace.remark);
		return same;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj==this){
			return true;
		}
		if(obj==null||!(obj instanceof Ace)){
			return false;
		}
		Ace ace=(Ace)obj;
		boolean same=
			AclUtil.equalString(this.actionType,ace.actionType)&&
			AclUtil.equalString(this.desAddr,ace.desAddr)&&
			AclUtil.equalString(this.desMatchType,ace.desMatchType)&&
			AclUtil.equalString(this.desMacAddr,ace.desMacAddr)&&
			AclUtil.equalString(this.desMacMatchType,ace.desMacMatchType)&&
			(AclUtil.equalString(this.desPort,ace.desPort)||
					AclUtil.equalString(this.desPort, PortTypeEnum.toggle(ace.desPort,ace.protocolType)))&&
			AclUtil.equalString(this.desWildcard,ace.desWildcard)&&
			AclUtil.compareEthernetType(this.ethernetType,ace.ethernetType)&&
			AclUtil.equalString(this.protocolType,ace.protocolType)&&
			AclUtil.equalString(this.sourceAddr,ace.sourceAddr)&&
			AclUtil.equalString(this.sourceMacAddr,ace.sourceMacAddr)&&
			AclUtil.equalString(this.sourceMacMatchType,ace.sourceMacMatchType)&&
			AclUtil.equalString(this.sourceMatchType,ace.sourceMatchType)&&
			(AclUtil.equalString(this.sourcePort,ace.sourcePort)||
					AclUtil.equalString(this.sourcePort,PortTypeEnum.toggle(ace.sourcePort,ace.protocolType)))&&
			AclUtil.equalString(this.sourceWildcard,ace.sourceWildcard)&&
			AclUtil.equalString(this.vlanNum,ace.vlanNum)&&
			AclUtil.equalString(this.timeRangeName,ace.timeRangeName);
		return same;
	}
	
	

	public String toString(){
//		String objectString = 
//			  "actionType=" + convert2String(this.getActionType()) + ";"
//			+ "protocolType=" + convert2String(this.getProtocolType()) + ";"
//			+ "vlanNum=" + convert2String(this.getVlanNum()) + ";"
//			+ "sourceMatchType=" + convert2String(this.getSourceMatchType()) + ";"
//			+ "sourceAddr=" + convert2String(this.getSourceAddr()) + ";"
//			+ "sourceWildcard=" + convert2String(this.getSourceWildcard()) + ";"
//			+ "sourceMacMatchType=" + convert2String(this.getSourceMacMatchType()) + ";"
//			+ "sourceMacAddr=" + convert2String(this.getSourceMacAddr()) + ";"
//			+ "sourcePort=" + convert2String(this.getSourcePort()) + ";"
//			+ "desMatchType=" + convert2String(this.getDesMatchType()) + ";"
//			+ "desAddr=" + convert2String(this.getDesAddr()) + ";"
//			+ "desWildcard=" + convert2String(this.getDesWildcard()) + ";"
//			+ "desMacMatchType=" + convert2String(this.getDesMacMatchType()) + ";"
//			+ "desMacAddr=" + convert2String(this.getDesMacAddr()) + ";"
//			+ "desPort=" + convert2String(this.getDesPort()) + ";"
//			+ "ethernetType=" + convert2String(this.getEthernetType()) + ";"
//			+ "timeRangeName=" + convert2String(this.getTimeRangeName()) + ";";
		
		StringBuffer buf = new StringBuffer();
		buf.append("actionType=").append(convert2String(this.getActionType()))
			.append(";protocolType=").append(convert2String(this.getProtocolType()))
			.append(";vlanNum=").append(convert2String(this.getVlanNum()))
			.append(";sourceMatchType=").append(convert2String(this.getSourceMatchType()))
			.append(";sourceAddr=").append(convert2String(this.getSourceAddr()))
			.append(";sourceWildcard=").append(convert2String(this.getSourceWildcard()))
			.append(";sourceMacMatchType=").append(convert2String(this.getSourceMacMatchType()))
			.append(";sourceMacAddr=").append(convert2String(this.getSourceMacAddr()))
			.append(";sourcePort=").append(convert2String(this.getSourcePort()))
			.append(";desMatchType=").append(convert2String(this.getDesMatchType()))
			.append(";desAddr=").append(convert2String(this.getDesAddr()))
			.append(";desWildcard=").append(convert2String(this.getDesWildcard()))
			.append(";desMacMatchType=").append(convert2String(this.getDesMacMatchType()))
			.append(";desMacAddr=").append(convert2String(this.getDesMacAddr()))
			.append(";desPort=").append(convert2String(this.getDesPort()))
			.append(";ethernetType=").append(convert2String(this.getEthernetType()))
			.append(";timeRangeName=").append(convert2String(this.getTimeRangeName()));
		
		return buf.toString();
	}
	
	public Ace cloneAce(Ace aceEntity){
		Ace ace = aceEntity;
		if (ace == null){
			ace = new Ace();
		}
		ace.setActionType(this.getActionType());
		ace.setDesAddr(this.getDesAddr());
		ace.setDesMacAddr(this.getDesMacAddr());
		ace.setDesMacMatchType(this.getDesMacMatchType());
		ace.setDesMatchType(this.getDesMatchType());
		ace.setDesWildcard(this.getDesWildcard());
		ace.setProtocolType(this.getProtocolType());
		ace.setSourceAddr(this.getSourceAddr());
		ace.setSourceMacAddr(this.getSourceMacAddr());
		ace.setSourceMacMatchType(this.getSourceMacMatchType());
		ace.setSourceMatchType(this.getSourceMatchType());
		ace.setSourceWildcard(this.getSourceWildcard());
		ace.setVlanNum(this.getVlanNum());
		ace.setTimeRangeName(this.getTimeRangeName());
		ace.setEthernetType(this.getEthernetType());
		ace.setSourcePort(this.getSourcePort());
		ace.setDesPort(this.getDesPort());
		ace.setRemark(this.getRemark());
		ace.setTimeRange(this.getTimeRange());
		ace.setAccessGroup(this.getAccessGroup());
		ace.setTemplate(this.getTemplate());
		return ace;
	}
	public Ace clone(EntityObject entityObject){
		Ace ace =  new Ace();
		ace.setActionType(this.getActionType());
		ace.setDesAddr(this.getDesAddr());
		ace.setDesMacAddr(this.getDesMacAddr());
		ace.setDesMacMatchType(this.getDesMacMatchType());
		ace.setDesMatchType(this.getDesMatchType());
		ace.setDesWildcard(this.getDesWildcard());
		ace.setProtocolType(this.getProtocolType());
		ace.setSourceAddr(this.getSourceAddr());
		ace.setSourceMacAddr(this.getSourceMacAddr());
		ace.setSourceMacMatchType(this.getSourceMacMatchType());
		ace.setSourceMatchType(this.getSourceMatchType());
		ace.setSourceWildcard(this.getSourceWildcard());
		ace.setVlanNum(this.getVlanNum());
		ace.setTimeRangeName(this.getTimeRangeName());
		ace.setEthernetType(this.getEthernetType());
		ace.setSourcePort(this.getSourcePort());
		ace.setDesPort(this.getDesPort());
		ace.setRemark(this.getRemark());
		ace.setTimeRange(this.getTimeRange());
		if (entityObject != null){
			if (entityObject instanceof AccessGroup){
				ace.setAccessGroup((AccessGroup)entityObject);
			}else if (entityObject instanceof Template){
				ace.setTemplate((Template)entityObject);
			}
		}else{
			ace.setAccessGroup(this.getAccessGroup());
			ace.setTemplate(this.getTemplate());
		}
		return ace;
	}

	public Ace clone(EntityObject entityObject, TimeRange timeRange){
		Ace ace = new Ace();
		ace.setActionType(this.getActionType());
		ace.setDesAddr(this.getDesAddr());
		ace.setDesMacAddr(this.getDesMacAddr());
		ace.setDesMacMatchType(this.getDesMacMatchType());
		ace.setDesMatchType(this.getDesMatchType());
		ace.setDesWildcard(this.getDesWildcard());
		ace.setProtocolType(this.getProtocolType());
		ace.setSourceAddr(this.getSourceAddr());
		ace.setSourceMacAddr(this.getSourceMacAddr());
		ace.setSourceMacMatchType(this.getSourceMacMatchType());
		ace.setSourceMatchType(this.getSourceMatchType());
		ace.setSourceWildcard(this.getSourceWildcard());
		ace.setVlanNum(this.getVlanNum());
		ace.setTimeRangeName(this.getTimeRangeName());
		ace.setEthernetType(this.getEthernetType());
		ace.setSourcePort(this.getSourcePort());
		ace.setDesPort(this.getDesPort());
		ace.setRemark(this.getRemark());
		ace.setTimeRange(timeRange);
		if (entityObject != null){
			if (entityObject instanceof AccessGroup){
				ace.setAccessGroup((AccessGroup)entityObject);
			}else if (entityObject instanceof Template){
				ace.setTemplate((Template)entityObject);
			}
		}else{
			ace.setAccessGroup(this.getAccessGroup());
			ace.setTemplate(this.getTemplate());
		}
		return ace;
	}
}
